namespace Library
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Windows.Forms;

    [ToolboxBitmap(typeof(Switch))]
    public class Switch : Control
    {
        private bool _bHaveAction = false;
        private string _ClickEvent = null;
        private string _DoubleEvent = null;
        private Bitmap _image1 = null;
        private Bitmap _image2 = null;
        private bool _infront = true;
        private string _linkName = null;
        private Bitmap _originalImage1 = null;
        private Bitmap _originalImage2 = null;
        private string _path1 = "";
        private string _path2 = "";
        private string _value1 = "0";
        private string _value2 = "0";
        public bool bHavePic1 = false;
        public bool bHavePic2 = false;
        private IContainer components = null;
        private Rectangle rectangele;

        public Switch()
        {
            this.InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public static string ImageToString(Image img)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                img.Save(stream, ImageFormat.Jpeg);
                return Convert.ToBase64String(stream.GetBuffer());
            }
        }

        private void InitializeComponent()
        {
            this.components = new Container();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            Pen pen;
            Graphics graphics = pe.Graphics;
            Rectangle rect = new Rectangle(new Point(0, 0), new Size(base.ClientSize.Width, base.ClientSize.Height));
            if (this.infront)
            {
                if (this._image1 == null)
                {
                    pen = new Pen(Color.Black, -1f);
                    graphics.DrawRectangle(pen, rect);
                    if (this._path1.Length != 0)
                    {
                        this.TheImage1 = (Bitmap) StringToImage(this._path1);
                    }
                }
                else
                {
                    if (!this.bHavePic1)
                    {
                        this.bHavePic1 = true;
                        this.TheImage1 = (Bitmap) StringToImage(this._path1);
                    }
                    graphics.DrawImage(this._image1, new Point(rect.X, rect.Y));
                    if ((this.Rect.Height != rect.Height) || (this.Rect.Width != rect.Width))
                    {
                        this.ResizeImage1(this.Rect.Width, this.Rect.Height);
                        this.Rect = rect;
                    }
                }
            }
            else if (this._image2 == null)
            {
                pen = new Pen(Color.Black, -1f);
                graphics.DrawRectangle(pen, rect);
                if (this._path2.Length != 0)
                {
                    this.TheImage2 = (Bitmap) StringToImage(this._path2);
                }
            }
            else
            {
                if (!this.bHavePic2)
                {
                    this.bHavePic2 = true;
                    this.TheImage2 = (Bitmap) StringToImage(this._path2);
                }
                graphics.DrawImage(this._image2, new Point(rect.X, rect.Y));
                if ((this.Rect.Height != rect.Height) || (this.Rect.Width != rect.Width))
                {
                    this.ResizeImage2(this.Rect.Width, this.Rect.Height);
                    this.Rect = rect;
                }
            }
            base.OnPaint(pe);
        }

        protected void ResizeImage1(int width, int height)
        {
            if (this._originalImage1 != null)
            {
                if (width < 1)
                {
                    width = 2;
                }
                if (height < 1)
                {
                    height = 2;
                }
                Bitmap bitmap = new Bitmap(this._originalImage1, new Size(width, height));
                this._image1 = (Bitmap) bitmap.Clone();
                bitmap.Dispose();
                this.Path1 = ImageToString(this._image1);
            }
        }

        protected void ResizeImage2(int width, int height)
        {
            if (this._originalImage2 != null)
            {
                if (width < 1)
                {
                    width = 2;
                }
                if (height < 1)
                {
                    height = 2;
                }
                Bitmap bitmap = new Bitmap(this._originalImage2, new Size(width, height));
                this._image2 = (Bitmap) bitmap.Clone();
                bitmap.Dispose();
                this.Path2 = ImageToString(this._image2);
            }
        }

        public static Image StringToImage(string sz)
        {
            byte[] buffer = Convert.FromBase64String(sz);
            MemoryStream stream = new MemoryStream();
            stream.Write(buffer, 0, buffer.Length);
            return Image.FromStream(stream);
        }

        public bool bHaveAction
        {
            get
            {
                return this._bHaveAction;
            }
            set
            {
                this._bHaveAction = value;
            }
        }

        public string ClickEvent
        {
            get
            {
                return this._ClickEvent;
            }
            set
            {
                this._ClickEvent = value;
            }
        }

        public string DoubleEvent
        {
            get
            {
                return this._DoubleEvent;
            }
            set
            {
                this._DoubleEvent = value;
            }
        }

        public bool infront
        {
            get
            {
                return this._infront;
            }
            set
            {
                this._infront = value;
                base.Invalidate();
            }
        }

        public string LinkName
        {
            get
            {
                return this._linkName;
            }
            set
            {
                this._linkName = value;
            }
        }

        [Browsable(false)]
        public string Path1
        {
            get
            {
                return this._path1;
            }
            set
            {
                this._path1 = value;
            }
        }

        [Browsable(false)]
        public string Path2
        {
            get
            {
                return this._path2;
            }
            set
            {
                this._path2 = value;
            }
        }

        [Browsable(false)]
        public Rectangle Rect
        {
            get
            {
                return this.rectangele;
            }
            set
            {
                this.rectangele = value;
            }
        }

        [Description("图片选择"), Category("图片")]
        public Bitmap TheImage1
        {
            get
            {
                return this._image1;
            }
            set
            {
                this.Rect = new Rectangle(new Point(0, 0), new Size(base.Width, base.Height));
                this._originalImage1 = value;
                this.ResizeImage1(this.rectangele.Width, this.rectangele.Height);
                base.Invalidate();
            }
        }

        [Category("图片"), Description("图片选择")]
        public Bitmap TheImage2
        {
            get
            {
                return this._image2;
            }
            set
            {
                this.Rect = new Rectangle(new Point(0, 0), new Size(base.Width, base.Height));
                this._originalImage2 = value;
                this.ResizeImage2(this.rectangele.Width, this.rectangele.Height);
                base.Invalidate();
            }
        }

        [Category("图片对应值"), Description("值1")]
        public string value1
        {
            get
            {
                return this._value1;
            }
            set
            {
                this._value1 = value;
                base.Invalidate();
            }
        }

        [Description("值2"), Category("图片对应值")]
        public string value2
        {
            get
            {
                return this._value2;
            }
            set
            {
                this._value2 = value;
                base.Invalidate();
            }
        }
    }
}

